﻿using Assets.Scripts.Enums;
using System.Collections.Generic;
using System.Linq;
using Assets.Scripts.UI;
using Test30.Engine;

namespace Assets.Scripts.App
{
    public class PlayerInputControl :Control, IInputHandler
    {
        public PlayerInputControl() : base(UiType.Normal,UiMode.NeedBack,string.Empty)
        {
            
        }
        public override void Init()
        {
            _game = Data as Game;
        }

        public IEnumerable<KeyInstruction> KeyInstructions
        {
            get
            {
                yield return new KeyInstruction("Help", new KeyInfo(Key.H, true));
                yield return new KeyInstruction("Exit", new KeyInfo(Key.Escape));
            }
        }

        public bool KeyDown(KeyInfo key)
        {
            Hero hero = _game.Hero;
            //PlayGameScreen screen = (PlayGameScreen)Screen;

            var handled = true;

            switch (key.Key)
            {
                case Key.Escape:
                    //screen.Quit();
                    UiRoot.Get.Log("escape");
                    break;

                case Key.I:
                    hero.SetNextAction(new WalkAction(hero, Direction.Nw));
                    break;
                case Key.O:
                    hero.SetNextAction(new WalkAction(hero, Direction.N));
                    break;
                case Key.P:
                    hero.SetNextAction(new WalkAction(hero, Direction.Ne));
                    break;
                case Key.K:
                    hero.SetNextAction(new WalkAction(hero, Direction.W));
                    break;
                case Key.L:
                    hero.SetNextAction(new WalkAction(hero, Direction.None));
                    break;
                case Key.Semicolon:
                    hero.SetNextAction(new WalkAction(hero, Direction.E));
                    break;
                case Key.Comma:
                    hero.SetNextAction(new WalkAction(hero, Direction.Sw));
                    break;
                case Key.Period:
                    hero.SetNextAction(new WalkAction(hero, Direction.S));
                    break;
                case Key.Slash:
                    hero.SetNextAction(new WalkAction(hero, Direction.Se));
                    break;


                default:
                    handled = false;
                    break;
            }

            if (Ui != null && !_game.Hero.Behavior.NeedsUserInput)
            {
                var control = Ui.CurrentControls.First(o => o is PlayGameScreen);
                ((PlayGameScreen) control).ProcessGame();
            }

            return handled;
        }

        public bool KeyUp(KeyInfo key)
        {
            return false;
        }
        private Game _game;
    }
}
